<template>
  <div>
    <el-form ref="ruleForm" :model="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="旧密码" prop="password" required>
        <el-input v-model.trim="ruleForm.password" type="password" maxlength="64" clearable />
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword" required>
        <el-input v-model.trim="ruleForm.newPassword" type="password" maxlength="64" clearable />
      </el-form-item>
      <el-form-item label="确认新密码" prop="newPassword1" required>
        <el-input v-model.trim="ruleForm.newPassword1" type="password" maxlength="40" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updatePsw">更新账户密码</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { updatePassword } from '@/api/admin'
import { mapGetters } from 'vuex' // eslint-disable-line

export default {
  data() {
    return {
      ruleForm: {
        password: '',
        newPassword: '',
        newPassword1: ''
      }
    }
  },
  computed: {
    ...mapGetters([
      'id'
    ])
  },
  methods: {
    updatePsw() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          const tempData = {}
          tempData.id = this.id
          tempData.password = this.ruleForm.password
          tempData.newPassword = this.ruleForm.newPassword
          updatePassword(tempData).then(response => {
            if (response.message === 'success') {
              this.$message({
                message: '编辑成功',
                type: 'success',
                duration: 2000
              })
              // getInfo()
            } else {
              this.handleWarning(response)
            }
          })
        }
      })
    },
    checkNickname(rule, value, callback) {
      if (!value) {
        return callback(new Error('请输入昵称！'))
      } else {
        callback()
      }
    },
    checkPhone(rule, value, callback) {
      if (!value) {
        return callback(new Error('请输入联系电话！'))
      } else {
        callback()
      }
    },
    handleWarning(response) {
      this.$message({
        message: response.message || response,
        type: 'warning',
        duration: 2000
      })
    }
  }
}
</script>
